package proxy;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author shxl2021
 * @Date 2023/7/24 16:50
 * @Version 1.0
 */
public class Manager implements Runnable {
    private static InheritableThreadLocal<String> local= new InheritableThreadLocal<>();

    public static void main(String[] args) throws Exception {

        local.set("t1");


        ThreadPoolExecutor executor = getExecutor();
        for (int i = 0; i < 100; i++) {
            executor.submit(new Manager());
        }

        Thread.sleep(100);
        local.remove();
        System.out.println("main get"+local.get());
        System.out.println("remove finish");
    }

    private static ThreadPoolExecutor getExecutor() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,
                3,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000),
                Thread::new,
                new ThreadPoolExecutor.AbortPolicy());
        return executor;

    }

    @Override
    public void run() {
        String p = Thread.currentThread().getName() + Thread.currentThread().getId();
        System.out.println("in:"+local.get()+"\t"+p);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("out:"+local.get()+"\t"+p);
        local.set(p+"son");
        System.out.println("reset:"+local.get()+"\t"+p);
    }
}
